#include "GhostLevel.h"
#include "../../../GameConfig.h"
#include "../../../Framework/SceneManager.h"
#include "../../../Scenes/Level/LevelManager.h"
#include "../../../Player/PlayerInfo.h"
#define kDistanceStar 10

GhostLevel::GhostLevel()
{

}

void GhostLevel::initalize(EGhostColor color, Vec2 position, float scale, int id)
{
	ID = id;

	GhostObject::initalize(color, position, scale);

	m_leftHand->setVisible(false);
	m_rightHand->setVisible(false);
	m_eyes->setVisible(false);
	m_mouth->setVisible(false);

	m_body->setTitleText(String::createWithFormat("%d", id)->getCString());
	m_body->setTitleFontSize(m_body->getContentSize().height / 3 * scale);
	m_body->setTitleColor(Color3B::BLACK);
	m_body->addClickEventListener(CC_CALLBACK_1(GhostLevel::tapGhost, this));

	initStar();
	setState(EGhostState::kGhostState_Default);
}

void GhostLevel::initStar()
{
	m_starLeft = Sprite::create("star_normal.png");
	m_starRight = Sprite::create("star_normal.png");
	m_starTop = Sprite::create("star_normal.png");

	m_starLeft->setScale(m_scale);
	m_starRight->setScale(m_scale);
	m_starTop->setScale(m_scale);

	float newWidthGhost = m_body->getContentSize().width*m_scale;
	float newHeightGhost = m_body->getContentSize().height*m_scale;
	m_starLeft->setPosition(m_body->getPosition().x - newWidthGhost / 2 - kDistanceStar, m_body->getPosition().y + newHeightGhost / 3);
	m_starRight->setPosition(m_body->getPosition().x + newWidthGhost / 2 + kDistanceStar, m_body->getPosition().y + newHeightGhost / 3);
	m_starTop->setPosition(m_body->getPosition().x, m_body->getPosition().y + newHeightGhost / 2 + kDistanceStar);

	this->addChild(m_starLeft);
	this->addChild(m_starRight);
	this->addChild(m_starTop);

}

void GhostLevel::tapGhost(cocos2d::Ref *pSender)
{
	if (!LevelManager::s_isActive)
	{
		if (!PlayerInfo::getInstance()->level->getLevelEnable(LevelManager::s_mode, ID))
			return;

		LevelManager::s_isActive = true;
		LevelManager::s_ID = ID;
		setState(EGhostState::kGhostState_Yeah);

		SceneManager::getInstance()->changeScene(this->getParent()->getParent()->getParent(), EScene::kScene_Play);
	}
}